﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace Pentakill
{
    public partial class Shipper : Form
    {

        SqlConnection con = new SqlConnection();
        public Shipper(string Shipperid)
        {
            InitializeComponent();
            string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            con.ConnectionString = constr;
            con.Open();
            LoadShipperInfo();
            DefaultShipperForm();
            int select=0;
            for (int i = 0; i < gvShipper.Rows.Count; i++)
            {
                DataGridViewRow dt = gvShipper.Rows[i];
                if (dt.Cells[0].Value.ToString()==Shipperid)
                {
                    select = i;
                    break;
                }
            }
            gvShipper.Rows[select].Selected = true;
        }
        #region Code Shippers

        bool ValidateShipperInfo()
        {
            bool error = false;
            if (mtxtShipperPhone.MaskCompleted == false)
            {
                errorProvider1.SetError(mtxtShipperPhone, "Please complete the required field \"Phone\"");
                error = true;
            }
            if (txtShipperCompany.Text.Length == 0)
            {
                errorProvider1.SetError(txtShipperCompany, "Please complete the required field \"Name\"");
                error = true;
            }
            if (error)
            {
                return false;
            }
            return true;
        }

        void DefaultShipperForm()
        {
            errorProvider1.Clear();
            errorProvider1.Clear();
            txtShipperid.Text="";
            txtShipperCompany.Text = "";
            mtxtShipperPhone.Text = "";
        }

        void LoadShipperInfo()
        {
            gvShipper.Rows.Clear();
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "Sales.Shippers_get";
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    gvShipper.Rows.Add(dr[0].ToString(), dr[1].ToString(), dr[2].ToString());
                }
                dr.Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            finally
            {
                con.Close();
                con.Open();
            }
        }

        void InsertNewShipper()
        {
            if (ValidateShipperInfo())
            {
                try
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = con;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "Sales.Shippers_Insert";
                    SqlParameter param = new SqlParameter("@Companyname", SqlDbType.NVarChar, 40);
                    param.Value = txtShipperCompany.Text;
                    cmd.Parameters.Add(param);
                    param = new SqlParameter("@phone", SqlDbType.NVarChar, 40);
                    param.Value = mtxtShipperPhone.Text;
                    cmd.Parameters.Add(param);
                    cmd.ExecuteNonQuery();
                    LoadShipperInfo();
                    MessageBox.Show("Insert successed!");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

        }
        
        void DeleteShipperInfo()
        {
            if (txtShipperid.Text!="")
            {
                try
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = con;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "Sales.Shippers_Delete";
                    SqlParameter param = new SqlParameter("@Shipperid", SqlDbType.Int);
                    param.Value = int.Parse(txtShipperid.Text);
                    cmd.Parameters.Add(param);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Delete successed!");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Have any data is used in other form\nCan not delete the Shipper", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    
                }

                LoadShipperInfo();
            }

        }

        void UpdateShipperInfo()
        {
            if (ValidateShipperInfo())
            {
                try
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = con;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "Sales.Shippers_Update";
                    SqlParameter param = new SqlParameter("@Shipperid", SqlDbType.Int);
                    param.Value = int.Parse(txtShipperid.Text);
                    cmd.Parameters.Add(param);
                    param = new SqlParameter("@Companyname", SqlDbType.NVarChar, 40);
                    param.Value = txtShipperCompany.Text;
                    cmd.Parameters.Add(param);
                    param = new SqlParameter("@phone", SqlDbType.NVarChar, 40);
                    param.Value = mtxtShipperPhone.Text;
                    cmd.Parameters.Add(param);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Update successed!");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                LoadShipperInfo();
            }

        }

        public string returnstringfororder()
        {
            return txtShipperid.Text;
        }

        #endregion


        private void btnAdd_Click(object sender, EventArgs e)
        {
            errorProvider1.Clear();
            if (ValidateShipperInfo())
            {
                InsertNewShipper();
                DefaultShipperForm();
            }
        }

        private void gvShipper_SelectionChanged(object sender, EventArgs e)
        {
            errorProvider1.Clear();
            btnAdd.Enabled = false;
            btnShipperDelete.Enabled = true;
            btnShipperUpdate.Enabled = true;
            txtShipperid.Enabled = true;
            if (gvShipper.SelectedRows.Count > 0)
            {
                DataGridViewRow dg = gvShipper.SelectedRows[0];
                txtShipperid.Text = dg.Cells[0].Value.ToString().Trim();
                txtShipperCompany.Text = dg.Cells[1].Value.ToString();
                mtxtShipperPhone.Text = dg.Cells[2].Value.ToString();
            }
        }

        private void btnShipperDelete_Click(object sender, EventArgs e)
        {
            errorProvider1.Clear();
            DialogResult rs = MessageBox.Show("Are you sure to delete?", "Confirm",
                MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (rs == DialogResult.Yes)
            {
                DeleteShipperInfo();
            }
        }

        private void btnShipperNew_Click(object sender, EventArgs e)
        {
            errorProvider1.Clear();
            DialogResult rs = MessageBox.Show("Are you sure to clear shipper?", "Confirm",
                   MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (rs == DialogResult.Yes)
            {
                DefaultShipperForm();
                btnAdd.Enabled = true;
                btnShipperDelete.Enabled = false;
                btnShipperUpdate.Enabled = false;
                txtShipperid.Enabled = false;
            }
        }

        private void btnShipperUpdate_Click(object sender, EventArgs e)
        {
            errorProvider1.Clear();
            if (ValidateShipperInfo())
            {

                DialogResult rs = MessageBox.Show("Are you sure to update?", "Confirm",
                       MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (rs == DialogResult.Yes)
                {
                    UpdateShipperInfo();
                }
            }
        }

        private void Shipper_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.Parent == null)
            {
                returnstringfororder();
            }
            else
            {
                this.Hide();
                this.Parent = null;
                e.Cancel = true;
            }
            con.Close();
        }

        private void txtShipperSearch_TextChanged(object sender, EventArgs e)
        {
            errorProvider1.Clear();
            if (txtShipperSearch.Text.Length == 0)
            {
                LoadShipperInfo();
                DefaultShipperForm();
            }
            else
            {
                DefaultShipperForm();
                DataGridViewRow dr;
                bool show;
                for (int i = 0; i < gvShipper.Rows.Count; i++)
                {
                    show = false;
                    dr = gvShipper.Rows[i];
                    dr.Visible = true;
                    dr.Cells[1].Style.BackColor = Color.White;
                    if (dr.Cells[1].Value.ToString().ToLower().Contains(txtShipperSearch.Text.ToLower()) == true)
                    {
                        dr.Cells[1].Style.BackColor = Color.Yellow;
                        show = true;
                    }
                    if (show == false)
                    {
                        dr.Visible = false;
                    }
                }
            }
        }
    }
}
